package com.nannybear.Controller;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import com.nannybear.R;
import com.nannybear.View.Timeline;

/**
 * @brief this class create the timeline on the BabyStateScreen and add rectangles
 */
public class TimelineController implements ScaleGestureDetector.OnScaleGestureListener{
    /********** DECLARATIONS **********/
    private ViewPuppeteer viewPuppeteer;
    private ScaleGestureDetector gestureScale;
    private float scaleFactor = 1;

    /************ METHODS ************/
    /**
     * @brief constructor which create the timeline
     * @param viewPuppeteer : takes the class ViewPuppeteer in parameter
     */
    public TimelineController(ViewPuppeteer viewPuppeteer){
        this.viewPuppeteer = viewPuppeteer;
        gestureScale = new ScaleGestureDetector(this.viewPuppeteer.getmActivity().getApplicationContext() ,this);
        addSpyToTimeline();

        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.green);
        this.addRectOnTimeline(R.color.red);
        this.addRectOnTimeline(R.color.orange);
        this.addRectOnTimeline(R.color.red);
    }

    /**
     * @brief add listener to the timeline to detect a pinch to zoom
     */
    public void addSpyToTimeline() {
        Timeline timeline = (Timeline) this.viewPuppeteer.getmActivity().findViewById(R.id.seekBar0);

        timeline.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                gestureScale.onTouchEvent(event);
                return true;
            }
        });
        this.viewPuppeteer.getmActivity().getBabyStateScreen().setTimeline(timeline);
    }


    public boolean onScaleBegin(ScaleGestureDetector detector){

        return true;
    }

    public boolean onScale(ScaleGestureDetector detector){
        scaleFactor *= detector.getScaleFactor();
        scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 8.0f));
        this.viewPuppeteer.getmActivity().getBabyStateScreen().getTimeline().zoomTimeline(scaleFactor);
        return true;
    }

    public void onScaleEnd(ScaleGestureDetector detector){
    }


    /**
     * @brief permit to add rectangles on the timeline
     * @param color : the color of the rectangle
     */
    private void addRectOnTimeline(int color){
        this.viewPuppeteer.getmActivity().getBabyStateScreen().getTimeline().addARect(color);
    }
}
